package com.alili_tv.system.controller;

import com.alili_tv.system.common.BaseContext;
import com.alili_tv.system.common.LoginRequired;
import com.alili_tv.system.common.R;
import com.alili_tv.system.entity.VideoFavorite;
import com.alili_tv.system.entity.VideoFavoriteFolder;
import com.alili_tv.system.exception.BusinessException;
import com.alili_tv.system.service.VideoFavoriteFolderService;
import com.alili_tv.system.service.VideoFavoriteService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 收藏的视频 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-03-17
 */
@RestController
@RequestMapping(value = "/videoFavor", produces = "application/json")
public class VideoFavoriteController {

    @Resource
    private VideoFavoriteFolderService vfFolderService;

    @Resource
    private VideoFavoriteService vfService;

    /**
     * 创建收藏夹
     *
     * @param vfFolder
     * @return
     */
    @LoginRequired
    @PostMapping("/createFolder")
    public R<String> createFolder(@RequestBody VideoFavoriteFolder vfFolder) {
        if (vfFolder.getName() == null || vfFolder.getName().trim().equals("")) {
            throw new BusinessException("文件名不能为空");
        }
        vfFolderService.save(vfFolder);
        return R.success("创建成功");
    }

    /**
     * 删除收藏夹
     *
     * @param folderId
     * @return
     */
    @LoginRequired
    @Transactional
    @PostMapping("/removeFolder/{folderId}")
    public R<String> removeFolder(@PathVariable Long folderId) {
        LambdaQueryWrapper<VideoFavoriteFolder> folderWrapper = new LambdaQueryWrapper<>();
        folderWrapper
                .eq(VideoFavoriteFolder::getCreateUser, BaseContext.getCurrentId())
                .eq(VideoFavoriteFolder::getId, folderId);
        boolean remove = vfFolderService.remove(folderWrapper);

        // 删除收藏夹下的视频
        if (remove) {
            LambdaQueryWrapper<VideoFavorite> vfWrapper = new LambdaQueryWrapper<>();
            vfWrapper.eq(VideoFavorite::getCreateUser, BaseContext.getCurrentId())
                    .eq(VideoFavorite::getFolderId, folderId);
            vfService.remove(vfWrapper);
        }
        return R.success("删除成功");
    }

    /**
     * 获取指定视频的在收藏夹下的收藏信息
     *
     * @param videoId
     * @return
     */
    @LoginRequired
    @GetMapping("/{videoId}")
    public R<List<VideoFavoriteFolder>> getFavorInfo(@PathVariable Long videoId) {
        List<VideoFavoriteFolder> folders = vfFolderService.getFavorInfo(BaseContext.getCurrentId(), videoId);
        return R.success(folders);
    }


    /**
     * 收藏视频
     *
     * @param folderIds
     * @param videoId
     * @return
     */
    @LoginRequired
    @Transactional
    @PostMapping("/addFavorite/{videoId}")
    public R<String> addFavorite(@RequestBody List<Long> folderIds, @PathVariable Long videoId) {
        LambdaQueryWrapper<VideoFavorite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(VideoFavorite::getCreateUser, BaseContext.getCurrentId())
                .eq(VideoFavorite::getVideoId, videoId);
        // 删除收藏夹中所有对应id的视频
        // delete from video_favorite where create_user = ? and video_id = ?
        vfService.remove(queryWrapper);

        // 添加视频到对应收藏夹
        if (folderIds != null && !folderIds.isEmpty()) {
            List<VideoFavorite> list = folderIds.stream().map(item -> {
                VideoFavorite videoFavorite = new VideoFavorite();
                videoFavorite.setVideoId(videoId);
                videoFavorite.setFolderId(item);
                return videoFavorite;
            }).toList();

            vfService.saveBatch(list);
        }
        return R.success("收藏成功");
    }

    /**
     * 删除指定收藏夹下的视频
     *
     * @param folderId
     * @param videoId
     * @return
     */
    @LoginRequired
    @DeleteMapping("/remove/{folderId}/{videoId}")
    public R<String> removeOne(@PathVariable Long folderId, @PathVariable Long videoId) {
        vfService.remove(
                new LambdaQueryWrapper<VideoFavorite>()
                        .eq(VideoFavorite::getCreateUser, BaseContext.getCurrentId())
                        .eq(VideoFavorite::getFolderId, folderId)
                        .eq(VideoFavorite::getVideoId, videoId)
        );
        return R.success("删除成功");
    }
}
